/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ucue.ptc.rest;

import com.google.gson.Gson;
import edu.ucue.ptc.gco.mdl.Cargo;
import edu.ucue.ptc.gco.mdl.Cliente;
import edu.ucue.ptc.gco.mdl.Empleado;
import edu.ucue.ptc.gco.ngc.ClienteManagerLocal;
import edu.ucue.ptc.gco.ngc.EmpleadoManagerLocal;
import edu.ucue.ptc.gco.ngc.UsuarioManagerLocal;
import edu.ucue.ptc.gco.util.AtributoInvalidoException;
import edu.ucue.ptc.gco.util.ClienteValidadorException;
import edu.ucue.ptc.gco.util.EmpleadoValidatorException;
import java.util.Date;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.PathParam;

/**
 * REST Web Service
 *
 * @author Valex
 */
@Path("cliente/{cedula}/{nombre}/{clave}/{callback}")
@RequestScoped
public class ClienteResource {

    @Context
    private UriInfo context;

     @Inject
    private UsuarioManagerLocal UsuarioManager;
    @Inject
    private ClienteManagerLocal ClienteManager;

    
    /**
     * Creates a new instance of ClienteResource
     */
    public ClienteResource() {
    }

    /**
     * Retrieves representation of an instance of edu.ucue.ptc.rest.ClienteResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/json")
    @Named
    public String getJson(@PathParam("callback") String callback, @PathParam("cedula") String cedula, @PathParam("nombre") String user,
            @PathParam("clave") String pass) throws ClienteValidadorException, AtributoInvalidoException, EmpleadoValidatorException {
        //TODO return proper representation object
        int IdPersona = UsuarioManager.verificarUsuario(user, pass);
        //String a=",\"valor\":\""+verificarUsuario;

        if (IdPersona != -1) {
           /* Cliente cli = new Cliente();
            cli.setCedulaRuc("0604043679");
            cli.setNombres("Valeria");
            cli.setApellidos("Haro");
            cli.setEstado(0);
            cli.setDescuento(5);
            ClienteManager.crear(cli);*/
            
            Cliente cliente = ClienteManager.buscarPorCedula(cedula,user,pass);

            if (cliente == null) {
                return callback + "({\"callback\":\"" + callback + "\",\"errormsg\":\"" + "Cliente no encontrado" + "\"})";
                //new JSONWithPadding("\",\"errormsg\":\""+"Usted no esta habilitado para esta aplicación",callback);
            } else {
                //     return "({\"callback\":\""+callback+"\",\"id\":\""+ruta.getIdRuta()+"\",\"fecha\":\""+ruta.getFecha()+"\"})";
                Gson json = new Gson();
                String j = json.toJson(cliente);
                return callback+"({\"callback\":\"" + callback + "\",\"cliente0\":{\"idCliente\":\"" + cliente.getIdPersona() + "\",\"nombre\":\"" + cliente.getNombres() + "\",\"apellido\":\"" + cliente.getApellidos()+ "\"}})";
                //  return new JSONWithPadding(j,callback);
            }
        }

        return "({\"callback\":\"" + callback + "\",\"errormsg\":\"" + "Usuario no registrado" + "\"})";
    }

    /**
     * PUT method for updating or creating an instance of ClienteResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/json")
    public void putJson(String content) {
        
    }
}
